// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

//const Container = require('Container');

cc.Class({
    extends: cc.Component,

    properties: {
        nineBlocks: {
            type:[cc.Node],
            default:[],
            tooltip:'九宫格各位置块节点组'
        },
        stones:{
            type:[cc.Node],
            default:[],
            tooltip:'红绿蓝三块石头，显示在右上角'
        },
        hiddenStones:{
            type:[cc.Node],
            default:[],
            tooltip:'消失的石头'
        },
        centerLights: {
            type:cc.Node,
            default:[],
            tooltip:'光面中间的指示灯'
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // 初始化镜子
        this.mirrors = [0,1,2,3,4,5];
        var count = 0;
        while (this.checkHasEqual(this.mirrors,[0,1,2,3,4,5])) {
            this.mirrors.sort(this.randomSort);
            count++
        }
        console.log('镜子刷新次数：'+count);
        // 初始化九宫格,对应索引的数是对应节点的位置
        this.rects = [0,1,2,3,4,5,6,7,8];
        var count = 0;
        var oldway = -1;
        this.transparentOne = Math.floor(Math.random()*9);
        var oppose = [1,0,3,2];
        while (this.checkRectsPush(this.rects,[0,1,2,3,4,5,6,7,8])) {
            var place = this.rects.indexOf(this.transparentOne);
            var way = Math.floor(Math.random()*4);
            if (way == oppose[oldway]) {
                this.way++;
                if (this.way > 3) this.way -=4;
            };
            this.oldway = this.way;
            switch (way) {
                case 0:
                    this.moveup(place);
                    break;
                case 1:
                    this.movedown(place);
                    break;
                case 2:
                    this.moveleft(place);
                    break;
                case 3:
                    this.moveright(place);
                    break;
            
                default:
                    break;
            }
            //this.rects.sort(this.randomSort);
            count++
        }
        console.log('格子刷新次数：'+count);
        this.resetPosition();
        this.rectCd = 0;
        this.onRectFinished = false;
        this.collectedStones = [false,false,false];
        this.rightAnswer = [0,1,2];
        this.rightAnswer.sort(this.randomSort);
        this.answer = [-1,-1,-1];
        
        for (let i = 0; i < 3; i++) {
            var HSL = this.centerLights[i].getComponent('HSL');
            HSL.H = this.rightAnswer[i]*120;
        };
    },

    start () {
        // 这一步放在此处是因为HSL在onload之前不能设
    },

    update (dt) {
        if (this.rectCd > 0) {
            this.rectCd -= dt;
            if (this.rectCd < 0) this.rectCd = 0;
        } else if (!this.onRectFinished && this.checkRectsOK()) {
            // 完成回调
            this.onRectFinished = true;
            var anim = this.nineBlocks[this.transparentOne].getComponent(cc.Animation);
            anim.summon = this.hiddenStones[0];
            anim.summonStone = function () {
                this.summon.active = true;
                AudioManager.playSe('出现', 0.9);
            };
            anim.play();
            AudioManager.playSe('闪烁', 0.9);
        }
    },

    // 设置镜子
    setMirror (index, photo) {
        this.mirrors[index] = photo;
        if (this.checkMirrorsOK()) {
            this.hiddenStones[1].active = true;
            AudioManager.playSe('出现', 0.9);
        }
    },

    // 检查镜子是否调对
    checkMirrorsOK () {
        return this.checkIsEqual(this.mirrors, [0,1,2,3,4,5]);
    },

    // 取得镜子图片
    getMirrorPhoto (index) {
        return this.mirrors[index];
    },

    // 检查格子是否调对
    checkRectsOK () {
        return this.checkIsEqual(this.rects, [0,1,2,3,4,5,6,7,8]);
    },

    // 初始打乱格子节点位置
    resetPosition () {
        for (let i = 0; i < 9; i++) {
            var pos = this.rects[i]; // 位置i对应的图片节点id
            var posX = 0, posY = 0;
            if (this.onRectLeft(i)) {
                posX = -80;
            } else if (this.onRectRight(i)) {
                posX = 80;
            };
            if (this.onRectDown(i)) {
                posY = -80;
            } else if (this.onRectUp(i)) {
                posY = 80;
            };
            this.nineBlocks[pos].x = posX;
            this.nineBlocks[pos].y = posY;
        };
        this.nineBlocks[this.transparentOne].opacity = 0;// 缺的那块透明
        //this.rectAnim = this.nineBlocks[this.transparentOne].getComponent(cc.Animation);
    },
    // 移动格子操作
    moveRect (dir) {
        if (this.rectCd > 0 && this.checkRectsOK) return;
        var place = this.rects.indexOf(this.transparentOne);
        switch (dir) {
            case 'up':
                if (this.onRectDown(place)) return;
                var upAction = cc.moveBy(0.6,0,80);
                var downAction = cc.moveBy(0.6,0,-80);
                this.nineBlocks[this.transparentOne].runAction(downAction);
                this.nineBlocks[this.rects[place+3]].runAction(upAction);
                // place down,place+3up
                this.rects[place] = this.rects[place+3];
                this.rects[place+3] = this.transparentOne;
                break;
            case 'down':
                if (this.onRectUp(place)) return;
                var upAction = cc.moveBy(0.6,0,80);
                var downAction = cc.moveBy(0.6,0,-80);
                this.nineBlocks[this.rects[place-3]].runAction(downAction);
                this.nineBlocks[this.transparentOne].runAction(upAction);
                // place up,place-3down
                this.rects[place] = this.rects[place-3];
                this.rects[place-3] = this.transparentOne;
                break;
            case 'left':
                if (this.onRectRight(place)) return;
                var leftAction = cc.moveBy(0.6,-80,0);
                var rightAction = cc.moveBy(0.6,80,0);
                this.nineBlocks[this.rects[place+1]].runAction(leftAction);
                this.nineBlocks[this.transparentOne].runAction(rightAction);
                // place right,place+1left
                this.rects[place] = this.rects[place+1];
                this.rects[place+1] = this.transparentOne;
                break;
            case 'right':
                if (this.onRectLeft(place)) return;
                var leftAction = cc.moveBy(0.6,-80,0);
                var rightAction = cc.moveBy(0.6,80,0);
                this.nineBlocks[this.transparentOne].runAction(leftAction);
                this.nineBlocks[this.rects[place-1]].runAction(rightAction);
                // place l,place-1 r
                this.rects[place] = this.rects[place-1];
                this.rects[place-1] = this.transparentOne;
                break;
            default:
                break;
        }
        this.rectCd = 0.6;
    },

    moveup(place){
        if (this.onRectDown(place)) return;
        this.rects[place] = this.rects[place+3];
        this.rects[place+3] = this.transparentOne;
    },

    movedown(place){
        if (this.onRectUp(place)) return;
        this.rects[place] = this.rects[place-3];
        this.rects[place-3] = this.transparentOne;
    },

    moveleft(place){
        if (this.onRectRight(place)) return;
        this.rects[place] = this.rects[place+1];
        this.rects[place+1] = this.transparentOne;
    },
    
    moveright(place){
        if (this.onRectLeft(place)) return;
        this.rects[place] = this.rects[place-1];
        this.rects[place-1] = this.transparentOne;
    },
    // 获得石头
    collect(id) {
        this.collectedStones[id] = true;
        this.stones[id].active = true;
    },
    // 移除石头
    removeStone(id, pos) {
        this.collect(id);
        this.centerLights[pos].active = false;
        this.centerLights[3].active = false;
        this.answer[pos] = -1;
    },
    // 放置石头
    useStone(id, pos) {
        this.collectedStones[id] = false;
        this.stones[id].active = false;
        this.answer[pos] = id;
        if (this.rightAnswer[pos] == id) {
            this.centerLights[pos].active = true;
            if (this.checkIsEqual(this.rightAnswer, this.answer)) this.centerLights[3].active = true;
        }
    },
    // 需要的方法
    checkHasEqual(arr1,arr2) {
        var len = arr2.length;
        for (var index = 0; index < len; index++) {
            if (arr1[index] == arr2[index]) return true;
            
        }
        return false;
    },

    checkRectsPush(arr1,arr2) {
        var len = arr2.length;
        var c = 0;
        for (var index = 0; index < len; index++) {
            if (arr1[index] == arr2[index]) c+=1;
            
        }
        return c > 1;
    },
    checkIsEqual(arr1,arr2) {
        var len = arr2.length;
        for (var index = 0; index < len; index++) {
            if (arr1[index] != arr2[index]) return false;
            
        }
        return true;
    },

    onRectLeft(id) {
        return id % 3 == 0;
    },
    onRectRight(id) {
        return id % 3 == 2;
    },
    onRectUp(id) {
        return Math.floor(id/3) == 0;
    },
    onRectDown(id) {
        return Math.floor(id/3) == 2;
    },
    
    // 随机排序
    randomSort(a, b) {
        return Math.random()>.5 ? -1 : 1; //通过随机产生0到1的数，然后判断是否大于0.5从而影响排序，产生随机性的效果。
    },

     //var arr = [1, 2, 3, 4, 5];
     //arr.sort(randomsort);
});
